package top.riske.imagebed.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * created by vital on 2025/3/5 上午8:55
 */
public class Result<T> {
    private static final Logger log = LoggerFactory.getLogger(Result.class);

    private long timestamp;// 时间戳
    private int code;// 状态码
    private boolean success;// 是否成功
    private String message;// 消息
    private T data;// 数据

    public Result() {
        this.code = 200;
        this.success = true;
        this.timestamp = System.currentTimeMillis();
    }

    public Result(int code, boolean success, String msg, T data) {
        this.code = code;
        this.success = success;
        this.message = msg;
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> Result<T> success() {
        return new Result<T>(200, true, "成功", null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<T>(200, true, "成功", data);
    }

    public static <T> Result<T> success(int code,String msg, T data) {
        return new Result<T>(code, true, msg, data);
    }

    public static <T> Result<T> success(String msg, T data) {
        return new Result<T>(200, true, msg, data);
    }

    public static <T> Result<T> fail() {
        return new Result<T>(400, false, "失败", null);
    }

    public static <T> Result<T> fail(String msg) {
        log.error("响应失败:{}",msg);
        return new Result<T>(400, false, msg, null);
    }

    public static <T> Result<T> fail(String msg, T data) {
        log.error("响应失败:{},数据：{}",msg,data);
        return new Result<T>(400, false, msg, data);
    }

    public static <T> Result<T> fail(int code, String msg) {
        log.error("响应失败:{},code:{}",msg,code);
        return new Result<T>(code, false, msg, null);
    }

    public static <T> Result<T> fail(int code, String msg,T data) {
        log.error("响应失败:{},code:{},数据：{}",msg,code,data);
        return new Result<T>(code, false, msg, data);
    }

    public Result<T> code(int code) {
        this.code = code;
        return this;
    }

    public Result<T> data(T data) {
        this.data = data;
        return this;
    }


    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
